package com.auerdi.ui.fragment.b_classify.child;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.auerdi.R;
import com.auerdi.ui.fragment.b_classify.child.utils.IndicatorLineUtil;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation_swipeback.SwipeBackFragment;

/**
 * Created by YoKeyword on 16/2/14.
 */
public class ADClaHomeFragMent extends SwipeBackFragment {

    Unbinder unbinder;
    @BindView(R.id.statusBar)
    View statusBar;
    @BindView(R.id.title_left)
    RelativeLayout titleLeft;
    @BindView(R.id.tab)
    TabLayout mTab;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    ArrayList<String> titles = new ArrayList();
    @BindView(R.id.title)
    TextView title;

    private int mCid;
    private String mTitle;

    public static ADClaHomeFragMent newInstance(int id, String title) {

        Bundle args = new Bundle();
        args.putInt("id", id);
        args.putString("title", title);
        ADClaHomeFragMent fragment = new ADClaHomeFragMent();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mCid = getArguments().getInt("id");
        mTitle = getArguments().getString("title");

        titles.add("综合");
        titles.add("销量");
        titles.add("价格");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.ad_cla_home, container, false);


        unbinder = ButterKnife.bind(this, view);

        title.setText(mTitle);
        View tpview = attachToSwipeBack(view);
        mTab.addTab(mTab.newTab());
        mTab.addTab(mTab.newTab());
        mTab.addTab(mTab.newTab());
        mTab.setupWithViewPager(viewPager);


        mTab.post(new Runnable() {
            @Override
            public void run() {
                IndicatorLineUtil.setIndicator(mTab, 40, 40);
            }
        });

//        viewPager.s
//        viewPager.set


        return tpview;
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        viewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {


            @Override
            public Fragment getItem(int position) {
                return ADTwoShowFragMentCL.newInstance(mCid, position);

            }

            @Override
            public int getCount() {
                return 3;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return titles.get(position);
            }
        });

    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

}
